package huoye.shops.huoye.mine.fragment;


import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;


import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.codeideology.android.utils.ApkUtils;
import com.codeideology.android.utils.LoginUtils;
import com.codeideology.android.widget.ios.dialog.NormalDialog;
import com.codeideology.android.widget.ios.dialog.listener.OnBtnClickL;

import java.math.BigDecimal;
import java.util.Timer;
import java.util.TimerTask;

import butterknife.BindView;
import butterknife.OnClick;
import commonality.updater.UpdateAppManager;
import huoye.shops.MainActivity;
import huoye.shops.R;
import huoye.shops.base.APIUrls;
import huoye.shops.base.BaseFragment;
import huoye.shops.base.UserManager;
import huoye.shops.guidance.LaunchActivity;
import huoye.shops.huoye.mine.activity.AboutActivity;
import huoye.shops.huoye.mine.activity.DividendActivity;
import huoye.shops.huoye.mine.activity.ManageAccountActivity;
import huoye.shops.huoye.mine.activity.ServiceSettingActivity;
import huoye.shops.huoye.mine.activity.ShopMessageActivity;
import huoye.shops.huoye.mine.activity.WithdrawActivity;
import huoye.shops.huoye.mine.presenter.MinePresenter;
import huoye.shops.huoye.mine.view.IMineView;
import huoye.shops.huoye.operation.activity.DiscountCancelActivity;
import huoye.shops.huoye.user.activity.LoginActivity;
import huoye.shops.huoye.user.model.UpdateInfo;
import huoye.shops.huoye.user.model.User;
import huoye.shops.utils.StockUtils;
import huoye.shops.widget.RoundImageView;
import huoye.shops.widget.UpdateDialog;

/**
 * A simple {@link Fragment} subclass.
 */
public class MineFragment extends BaseFragment implements IMineView{


    @BindView(R.id.mine_rv_head)
    RoundImageView rvHead;
    @BindView(R.id.mine_tv_versions)
    TextView tvVersions;
    @BindView(R.id.mine_tv_shop_name)
    TextView tvShopName;
    @BindView(R.id.mine_tv_balance)
    TextView tvBalance;
    @BindView(R.id.mine_tv_inspect_version)
    TextView tvInspectVersions;

    private MinePresenter mMinePresenter;


    @OnClick(R.id.mine_iv_more)
    void onMoreClick(){
        outAccound();
    }
    @OnClick(R.id.mine_btn_withdraw)
    void onWithdrawClick(){
        startActivity(WithdrawActivity.class);
    }
    @OnClick(R.id.mine_ll_balance)
    void onBalanceClick(){
       // makeToast("3");
    }

    @OnClick(R.id.mine_ll_service_setting)
    void onServiceSettingClick(){
       startActivity(ServiceSettingActivity.class);
    }

    @BindView(R.id.mine_ll_manage_user)
    LinearLayout llManageUser;

    @BindView(R.id.mine_ll_dividend)
    LinearLayout llDividend;



    @OnClick(R.id.mine_ll_manage_user)
    void onManageUserClick(){
        startActivity(ManageAccountActivity.class);
    }
    @OnClick(R.id.mine_ll_message_shop)
    void onMessageShopClick(){
        startActivity(ShopMessageActivity.class);
    }

    @OnClick(R.id.mine_ll_dividend)
    void onDividendClick(){
        startActivity(DividendActivity.class);
    }

    @OnClick(R.id.mine_ll_about)
    void onAboutClick(){
        startActivity(AboutActivity.class);
    }

    @OnClick(R.id.mine_ll_version)
    void onVersionClick(){
        mMinePresenter.checkUpdate();
    }


    @Override
    public int getContentViewId() {
        return R.layout.fragment_mine;
    }

    @Override
    protected void initView(View view, @Nullable Bundle bundle) {


        mMinePresenter = new MinePresenter();
        mMinePresenter.addTaskListener(this);
        mMinePresenter.onStart();
        mMinePresenter.getUser();

        String versionName = ApkUtils.getVersionName(getActivity());

        tvInspectVersions.setText(versionName);

        boolean powerManage = UserManager.getInstance().getPowerManage();
        if(powerManage){
            llManageUser.setVisibility(View.VISIBLE);
            llDividend.setVisibility(View.VISIBLE);
        }else {
            llManageUser.setVisibility(View.GONE);
            llDividend.setVisibility(View.GONE);

        }
    }


    @Override
    public void onResume() {
        super.onResume();
        if(mMinePresenter != null){
            mMinePresenter.getUser();
        }
    }

    //退出账户
    private void outAccound(){
        final NormalDialog dialog = new NormalDialog(getActivity());
        dialog.isTitleShow(false);
        dialog.content("确定退出当前账号?")//
                .style(NormalDialog.STYLE_TWO)//
                .titleTextSize(23)//
                .widthScale(0.75f)
                .show();

        dialog.setOnBtnClickL(
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {
                        dialog.dismiss();
                    }
                },
                new OnBtnClickL() {
                    @Override
                    public void onBtnClick() {

                        UserManager.getInstance().logout();
                        dialog.dismiss();
                        //LoginUtils.startActivity(getActivity(), MainActivity.class);
                        Intent intent = new Intent(getActivity(), LoginActivity.class);
                        intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_CLEAR_TASK);
                        startActivity(intent);

                    }
                });
    }

    @Override
    public void getUserData(User user) {


        //测试数据
        if(user.isBasicsVersion()){
            tvVersions.setText("基础版");
            tvVersions.setSelected(false);
            tvVersions.setBackgroundResource(R.mipmap.img_versions_basics);
        }else {
            tvVersions.setText("豪华版");
            tvVersions.setSelected(true);
            tvVersions.setBackgroundResource(R.mipmap.img_versions_deluxe);
        }



        Glide.with(this)
                .load(APIUrls.COMMON_CONFIG.TYPE_COMMON_ALIYUN+user.getUserImg())
                .dontAnimate()
                .placeholder(R.mipmap.ic_error)
                .error(R.mipmap.ic_error)
                .diskCacheStrategy(DiskCacheStrategy.NONE)
                .into(rvHead);
        UserManager.getInstance().saveShopLogo(user.getUserImg());


        tvBalance.setText(user.getBalance()+"");
        tvShopName.setText(user.getShopName());

    }

    @Override
    public void onCheckUpdate(UpdateInfo info) {
        // 检查升级
        showUpdate(info);
    }


    /**
     * 升级提示
     *
     * @param info
     */
    private void showUpdate(final UpdateInfo info) {
        String versionName = ApkUtils.getVersionName(getActivity());
        Log.e("test",versionName);
        if(Float.valueOf(versionName) >= Float.valueOf(info.getAppVerno())){
            makeToast("当前已是最新版本");
        }else {
            showDialogUpdater(info);
        }
    }





    private void showDialogUpdater(UpdateInfo info){
        UpdateDialog updateDialog = new UpdateDialog(getActivity());
        updateDialog.setOnUpdateListener(new UpdateDialog.OnUpdateListener() {
            @Override
            public void onUpdateClick() {
                update(info);
            }
        });
        updateDialog.show();
        updateDialog.setDialogContent(info.getAppVerno()+"",info.getSize(),info.getUpContent());
    }

    private void update(UpdateInfo info){
        UpdateAppManager.downloadApkS(getActivity(), info.getDownUrl(), getResources().getString(R.string.app_name), "帮播侠");
    }



}
